package com.astrocsr.dao;

import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.astrocsr.domain.bean.TblUserRole;

@Repository("userRoleDAO")
public class UserRoleDAO extends BaseHelperDAO {

	public static final String USER_ID 	    = "userId";
	
	public List<TblUserRole> findByUserId(Object userId) throws Exception {
		return findByProperty(USER_ID, userId);
	}

	@SuppressWarnings("unchecked")
	public List<TblUserRole> findByProperty(String propertyName, Object value) throws Exception {

		try {

			String queryString = "from TblUserRole as model where model." + propertyName + "= :propertyName";
			Query query = getCurrentSession().createQuery(queryString);
			query.setParameter("propertyName", value);
			return query.list();

		} catch (RuntimeException re) {
			throw re;
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<TblUserRole> listUserRoleByRoleCode(String roleCode) throws Exception {
		List<TblUserRole> result = null;
		
		String queryString = "from TblUserRole where roleCode = :roleCode"; // Hibernate Query Language
		result = getCurrentSession().createQuery(queryString).setParameter("roleCode", roleCode).list();
		
		return result;
	}
	
	/**
	 * Insert customer into database
	 */
	public void addUser(TblUserRole entity) throws Exception {
		// Insert into database
		getCurrentSession().save(entity);
	}
}